package com.dd.cloud.user.service.educationvisit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.user.entity.educationvisit.PatientEducationVisitTag;
import com.dd.cloud.user.mapper.patient.PatientEducationVisitTagMapper;
import com.dd.cloud.user.service.educationvisit.IPatientEducationVisitTagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 患教文章标签表 服务实现类
 * </p>
 *
 * @author czg
 * @since 2024-03-02
 */
@Service
public class PatientEducationVisitTagServiceImpl extends ServiceImpl<PatientEducationVisitTagMapper, PatientEducationVisitTag> implements IPatientEducationVisitTagService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPatientEducationTag(String name, Integer type) {
        PatientEducationVisitTag tag = new PatientEducationVisitTag();
        tag.setTagName(name)
                .setCreateDate(LocalDateTime.now())
                .setType(type)
                .setStatus(1);
        this.save(tag);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePatientEducationTag(Integer tagId, String name) {
        PatientEducationVisitTag tag = this.getById(tagId);
        if (tag == null) {
            throw new ZekeException("记录为空");
        }
        tag.setTagName(name);
        Boolean flag = this.updateById(tag);
        if (!flag) {
            throw new ZekeException("更新失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTagStatus(Integer tagId) {
        PatientEducationVisitTag tag = this.getById(tagId);
        if (tag == null) {
            throw new ZekeException("记录为空");
        }
        tag.setStatus(-1);
        Boolean flag = this.updateById(tag);
        if (!flag) {
            throw new ZekeException("更新失败");
        }
    }

    @Override
    public List<PatientEducationVisitTag> queryTagList(Integer type) {
        LambdaQueryWrapper<PatientEducationVisitTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PatientEducationVisitTag::getType, type)
                .eq(PatientEducationVisitTag::getStatus, 1)
                .orderByAsc(PatientEducationVisitTag::getCreateDate);
        List<PatientEducationVisitTag> list = this.list(queryWrapper);
        return list;
    }
}
